package com.example.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.example.POJO.User;
import com.example.service.AccountService;
import com.example.POJO.Account;
import com.example.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/account")
public class AccountController {
    // inject account service
    @Autowired
    private AccountService accountService;
    @Autowired
    private UserService userService;
    @GetMapping(value = "/getAccount", consumes = "application/json;charset=UTF-8")
    public String updatePassword() {
        try {
            List<Account> res=accountService.getAllAccounts();
            // 返回成功响应
            return res.toString();
        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            return "false";
        }
    }
    @PostMapping(value = "/accountAlter", consumes = "application/json;charset=UTF-8")
    public String updatePassword(@RequestParam HashMap<String, String> data) {
        try {
            String password = data.get("password");
            Integer id = Integer.valueOf(data.get("id"));
            Account account = new Account();
            account.setId(id);
            account.setPassword(password);
            accountService.updatePassword(account);
            // 返回成功响应
            return "true";
        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            return "false";
        }
    }

    @PostMapping(value = "/register", consumes = "application/json;charset=UTF-8")
    public String register(@RequestBody HashMap<String, String> data) {
        try {
            System.out.println(data);
            // 在这里，您可以访问从客户端发送的数据
            String name = data.get("name");
            String phone = data.get("phone");
            //如果账号已存在，返回false
            if (accountService.phoneIsExit(phone)) {
                return "false";
            }
            String password = data.get("password");
            String sex = data.get("sex");
            String birth = data.get("birth");
            String identity = data.get("identy");

            Account account = new Account();
            User user = new User();
            account.setPhone(phone);
            account.setPassword(password);
            user.setName(name);
            user.setSex(sex);
            user.setBirth(birth);
            user.setIdenty(identity);
            // 进行业务逻辑处理，存储到数据库等
            System.out.println(account);
            accountService.insertAccount(account);
            Integer id = accountService.getIdByPhone(account.getPhone());
            System.out.println(id);
            user.setId(id);
            userService.insertUser(user);
            // 返回成功响应
            return "true";
        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            System.out.println(e);
            return "false";
        }
    }

    @PostMapping(value = "/log", consumes = "application/json;charset=UTF-8")
    public String log(@RequestBody HashMap<String, String> data) {
        try {
            System.out.println(data);
            // 在这里，您可以访问从客户端发送的数据
            String phone = data.get("phone");
            String password = data.get("password");
            if (phone.equals("") || password.equals("")) {
                return null;
            }

            // 进行业务逻辑处理，存储到数据库等
            String getPassword = accountService.getPasswordByPhone(phone);
            if (getPassword.equals(password)) {
                return "true";
            } else {
                return "false";
            }
        } catch (Exception e) {
            System.out.println(e);
            // 处理异常，返回相应的错误响应
            return "false";
        }
    }

    @PostMapping("/unityRegister")
    public String unityRegister(@RequestBody Account account) {
        try {
            if (accountService.phoneIsExitUnity(account.getPhone())) {
                return "false";
            }
            // 进行业务逻辑处理，存储到数据库等
            accountService.insertUnityAccount(account);
            // 返回成功响应
            return "true";
        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            System.out.println(e);
            return "false";
        }
    }

    @PostMapping("/unityLogin")
    public String unityLogin(@RequestBody Account account) {
        try {
            System.out.println(account.getPhone());
            System.out.println(account.getPassword());
            if (!accountService.phoneIsExitUnity(account.getPhone())) {
                System.out.println(1);
                return "false";
            }
            if (!accountService.boolPasswordUnity(account)) {
                System.out.println(2);
                return "false";
            }
            // 返回成功响应
            return "true";
        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            System.out.println(e);
            return "false";
        }
    }

    @PostMapping("/unityUpdate")
    public String unityUpdate(@RequestBody Account account) {
        try {
            if (!accountService.phoneIsExitUnity(account.getPhone())) {
                return "false";
            }
            accountService.updatePasswordUnity(account);
            // 返回成功响应
            return "true";
        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            System.out.println(e);
            return "false";
        }
    }
}

